﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;

namespace Vision.Core.ActivityComponents.SharePoint
{
    /// <summary>
    /// 
    /// </summary>
    public class SPListUpdateListItem : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [DefaultValue("")]
        public Command ListCommand { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DefaultValue("")]
        [Category("Input")]
        public InArgument<string> ListName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [DefaultValue("")]
        public InArgument<Dictionary<string,string>> ListFields { get; set;}

        /// <summary>
        /// 
        /// </summary>
        [Category("Output")]
        [DefaultValue("")]
        public OutArgument<string> ListItemId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            Dictionary<string, string> fields = this.ListFields.Get(context);
            
            StringBuilder xmlBuilder = new StringBuilder();
            xmlBuilder.AppendLine("<Method ID='1' Cmd='" + this.ListCommand.ToString() + "'>");
            foreach (string field in fields.Keys){
                xmlBuilder.AppendLine("<Field Name='" + field + "'>" + fields[field] + "</Field>");
            }
            xmlBuilder.AppendLine("</Method>");            
                        
            XmlElement xmlElement = new XmlDocument().CreateElement("Batch");
            xmlElement.SetAttribute("OnError", "Continue");
            xmlElement.SetAttribute("ListVersion", "1");
            xmlElement.InnerXml = xmlBuilder.ToString();

            List<object> methodParams = new List<object>();
            methodParams.Add(this.ListName.Get(context));
            methodParams.Add(xmlElement);

            Components.DynamicWebService spService = (Components.DynamicWebService)context.Properties.Find("F1634880-5000-11E0-A748-B487DFD72085");
            if (spService != null){
                XmlElement result = spService.InvokeMethod<XmlElement>("UpdateListItems", methodParams);
                if (result != null){                                        
                    this.ListItemId.Set(context, result.FirstChild.ChildNodes[2].Attributes["ows_ID"].Value);                                        
                }
            }                        
        }

        /// <summary>
        /// 
        /// </summary>
        public enum Command
        {
            New,
            Update,
            Delete
        }
    }
}
